//
//  AsteroidsAppDelegate.m
//  Asteroids
//
//  Created by Charles@Kollar.com on 8/13/10.
//
//
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

#import "cocos2d.h"

#import "AsteroidsAppDelegate.h"
#import "AsteroidsLayer.h"
#import "SaveScoreLayer.h"
#import "DetailOverlayLayer.h"
#import "ApplicationData.h"


@implementation AsteroidsAppDelegate

@synthesize window;


//
// 
// The UIApplicatiodDelegate methods provide application 'life cycle' hooks for the application to tie into.
// For further information on these methods, please see the UIApplication Delegate Protocol Reference
// http://developer.apple.com/iphone/library/documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html

//
// Called when the application is launched
- (void) applicationDidFinishLaunching:(UIApplication*)application
{
	NSLog(@"AsteroidsAppDelegate applicationDidFinishLaunching");
	// CC_DIRECTOR_INIT()
	//
	// 1. Initializes an EAGLView with 0-bit depth format, and RGB565 render buffer
	// 2. EAGLView multiple touches: disabled
	// 3. creates a UIWindow, and assign it to the "window" var (it must already be declared)
	// 4. Parents EAGLView to the newly created window
	// 5. Creates Display Link Director
	// 5a. If it fails, it will use an NSTimer director
	// 6. It will try to run at 60 FPS
	// 7. Display FPS: NO
	// 8. Device orientation: Portrait
	// 9. Connects the director to the EAGLView
	//
	CC_DIRECTOR_INIT();
	
	// Obtain the shared director in order to...
	CCDirector *director = [CCDirector sharedDirector];
	
	// Sets landscape mode
	[director setDeviceOrientation:kCCDeviceOrientationLandscapeLeft];
	
	// Turn on display FPS
	[director setDisplayFPS:YES];
	
	// Preload the sounds...
	[[SimpleAudioEngine sharedEngine] preloadEffect: MISSILE_HIT_ASTEROID_WAV];
	
	// Turn on multiple touches
	EAGLView *view = [director openGLView];
	[view setMultipleTouchEnabled:YES];
	
	// Default texture format for PNG/BMP/TIFF/JPEG/GIF images
	// It can be RGBA8888, RGBA4444, RGB5_A1, RGB565
	// You can change anytime.
	[CCTexture2D setDefaultAlphaPixelFormat:kTexture2DPixelFormat_RGBA8888];

	// Note that 'scene' and 'layer' are autorelease objects.
	CCScene *scene = [CCScene node];
	AsteroidsLayer *layer = [AsteroidsLayer node];
	
	// add layer as a child to scene
	[scene addChild: layer];
	
	// Tell the director to make this the opening scene of the game...
	[director runWithScene: scene];
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
	NSLog(@"AsteroidsAppDelegate aapplicationDidBecomeActive");
	// Contine calling the scheduled target/selector (see 'pause')
	[[CCDirector sharedDirector] resume];
	[[ApplicationData shared] reload];
}

- (void)applicationWillResignActive:(UIApplication *)application {
	NSLog(@"AsteroidsAppDelegate applicationWillResignActive");
	// Director now won’t call any scheduled target/selector ours is set
	// at the bottom of the init method in the implementation for AsteroidsScene
	[[CCDirector sharedDirector] pause];
	[[ApplicationData shared] save];
}

// Need to free up memory now, or the application may terminate without warning...
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
	NSLog(@"AsteroidsAppDelegate applicationDidReceiveMemoryWarning");
	[[CCDirector sharedDirector] purgeCachedData];
}

// Save any data that needs to be use to reconstitute the app as it is because the
// program is to be stopped and removed from memeory....
// This will typically involve the user of setting [NSUserDefaults standardUserDefaults]
// and then restoring them in the applicationDidFinishLaunching method.
- (void)applicationWillTerminate:(UIApplication *)application {
	NSLog(@"AsteroidsAppDelegate applicationWillTerminate");
	[[ApplicationData shared] save];
	// Calling the CCDirector sharedDirector end method causes the program not to be able
	// to restart after terminating (on the device). Possibly there is a bug in the current
	// cocos2d-iphone version. It works fine on the simulator.
//	[[CCDirector sharedDirector] end];
}

// Things like: it's midnight!, changed to/from daiylight savings time,
// the time on the clock for the device changed for some reason.
- (void)applicationSignificantTimeChange:(UIApplication *)application {
	NSLog(@"AsteroidsAppDelegate applicationSignificantTimeChange");
	[[CCDirector sharedDirector] setNextDeltaTimeZero:YES];
}

- (void)dealloc {
	NSLog(@"AsteroidsAppDelegate dealloc");
	[window release];
	[super dealloc];
}

@end
